package com.project.slsx.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.project.slsx.R;
import com.project.slsx.bean.GoodsBean;

import java.util.List;

public class GuadanAdapter extends BaseQuickAdapter<List<GoodsBean>, BaseViewHolder> {

    private View guadanHeader;

    private Context mContext;

    public GuadanAdapter(Context c, int layoutResId) {
        super(layoutResId);
        mContext = c;
    }

    @Override
    protected void convert(BaseViewHolder holder, List<GoodsBean> item) {
        RecyclerView recyclerView = holder.getView(R.id.recyclerview_guadan_detail);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        GuadanDetailAdapter adapter = new GuadanDetailAdapter(R.layout.layout_item_guadan_detail);
        guadanHeader = View.inflate(mContext, R.layout.layout_item_guadan_header, null);
        recyclerView.setAdapter(adapter);
        adapter.setHeaderView(guadanHeader);
        adapter.setNewData(item);
        holder.setText(R.id.tv_guadan_order, "第 " + String.valueOf(holder.getLayoutPosition() + 1) + " 单");
        holder.setText(R.id.tv_guadan_total_number, item.size() + "");
        int size = item.size();
        double money = 0;
        for (int i = 0; i < size; i++) {
            GoodsBean goodsBean = item.get(i);
            money += goodsBean.isZengsong() ? 0 : goodsBean.getNumber() * goodsBean.getPrice();
        }
        holder.setText(R.id.tv_guadan_total_money, String.valueOf(money));
        ImageView delete = holder.getView(R.id.iv_guadan_delete);
        delete.setOnClickListener(v -> {
            if (onDeleteClickListener != null) {
                onDeleteClickListener.OnDeleteClickListener(holder.getLayoutPosition());
            }
        });

    }

    public interface onDeleteClickListener {
        void OnDeleteClickListener(int position);
    }

    private onDeleteClickListener onDeleteClickListener;

    public GuadanAdapter.onDeleteClickListener getOnDeleteClickListener() {
        return onDeleteClickListener;
    }

    public void setOnDeleteClickListener(GuadanAdapter.onDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }
}
